package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserContorller {
    @Autowired(required = false)
    private UserService userService;
    @RequestMapping("all")
    public String all(Model model){
        List<User> users=userService.selectUserByName();
        System.out.println("===");
        System.out.println(users);
        model.addAttribute("users",users);
        return "users";
    }
    @RequestMapping("AddUser")
    public ModelAndView AddUser(User user, Model model){
        boolean b = userService.addUser(user);
        if (b){
            System.out.println("新增成功");
        }else {
            System.out.println("失败");
        }
        ModelAndView mav = new ModelAndView("redirect:/all");
        return mav;
    }
    @RequestMapping("Add1")
    public ModelAndView Add1() {
        return new ModelAndView("add");
    }

    @GetMapping(value = "delete/{id}")
    public ModelAndView delete(@PathVariable int id) {
        userService.deleteById(id);
        ModelAndView mav = new ModelAndView("redirect:/all");
        return mav;
    }
}
